﻿using ComLib.ComFun;
using IPrinting;
using LogLib;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Reflection;

namespace EXPrinter
{
    public class ExyPrinter : IPhotoPrint
    {
        private ExyOption _option = new ExyOption();

        private string _error = "";
        public string ErrorMessage
        {
            get
            {
                return _error;
            }
        }

        private string _appName = Assembly.GetExecutingAssembly().GetName().Name;
        public string Name
        {
            get
            {
                return _appName;
            }
        }

        public int TimeoutMillisecond
        {
            get;
            set;
        }

        public int ID
        {
            set;
            get;
        }

        public bool Inited
        {
            get;
            private set;
        }

        public bool Exited
        {
            get;
            private set;
        }

        public ExyPrinter()
        {
            LoadConfig();
        }

        private void LoadConfig()
        {
            if (!File.Exists(IniConfigFile.IniFileName))
            {
                _option.Save();
            }
            _option.Load();
        }

        public bool Init(string param, out string outParam)
        {
            outParam = "";
            if (!CommonPhotoPrintDll.bInit)
            {
                IntPtr intPtr = new IntPtr();
                int nRet = CommonPhotoPrintDll.InitPersoDLL(intPtr, 0, IniConfigFile.IniFileName);
                if (0 == nRet)
                {
                    CommonPhotoPrintDll.bInit = true;
                    Inited = true;
                    Exited = false;
                }
                else
                {
                    Inited = false;
                    Exited = false;
                    CommonPhotoPrintDll.bInit = false;
                    _error = "初始化照片打印动态库失败";
                }
            }
            return Inited;
        }

        public void Exit()
        {
            if (CommonPhotoPrintDll.bInit)
            {
                CommonPhotoPrintDll.UnInitPersoDLL();
                CommonPhotoPrintDll.bInit = false;
                Exited = true;               
            }         
        }

        public bool Print(string param, out string outParam)
        {
            outParam = "";
            JObject inParams = JObject.Parse(param);
            ReportPrintInfo info = new ReportPrintInfo();
            info.YWBH = (string)inParams["PhotoName"];
            info.PicPath = (string)inParams["PhotoPath"];
            string strLog = string.Format("开始打印照片: 条形码:{0}, 照片路径: {1}", info.YWBH, info.PicPath);
            Log.WriteAsync(strLog);
            int ret = CommonPhotoPrintDll.DoPrint(info);
            if (ret == 0 || ret == 1)
            {              
                Log.WriteAsync("打印完成");
                return true;
            }
            else
            {
                _error = string.Format("打印失败, 错误代码:{0}", ret);
                Log.WriteAsync(_error);
                return false;
            }            
        }

        public bool Clear(string param, out string outParam)
        {
            outParam = "";
            PrinterService.ClearPrintQueue(_option.Printer);
            return true;
        }

        public void ShowConfigDialog(string inParam)
        {
            FormConfig form = new FormConfig();
            form.ShowDialog();
        }
    }
}
